var {
  find,
  findOneByid,
  createOne,
  count,
  remove,
  groupBy,
  updateOne,
} = require("../dao/base.js");
var boardsModel = require("../models/boards");
var administratorsModel = require("../models/admin");

//创建公告
module.exports.createOneBoard = async function (req, res, next) {
  let loginUserid = req.query.loginUserid;
  var doc = await findOneByid(administratorsModel, { userid: loginUserid });
  let needRole = '公告管理员';
  let checkPermission = doc.permission.includes(needRole)
  ||doc.permission.includes('超级管理员')
  if(!checkPermission){
    return  res.sendResult(doc, 403, "你没有权限");
  }
  var data = {
    title: req.body.title,
    content: req.body.content,
  };
  var doc = await createOne(boardsModel, data);
  if (doc) {
    res.sendResult(doc, 200, "公告发布成功！");
  } else {
    res.sendResult(doc, 500, "公告发布失败！");
  }
};

// 创建公告
module.exports.deleteOneBoard = async function (req, res, next) {
  let loginUserid = req.query.loginUserid;

  var doc = await findOneByid(administratorsModel, { userid: loginUserid });

  let needRole = '公告管理员';
  let checkPermission = doc.permission.includes(needRole)
  ||doc.permission.includes('超级管理员')
  if(!checkPermission){
    return res.sendResult(doc, 403, "你没有权限");
  }
  var data = {
    _id: req.query._id,
  };
  console.log(data)
  var doc = await remove(boardsModel, data);
  if (doc) {
    res.sendResult(doc, 200, "删除公告成功！");
  } else {
    res.sendResult(doc, 500, "删除公告失败！");
  }
};

//创建公告
module.exports.getBoards = async function (req, res, next) {
  var doc = await find(boardsModel);
  if (doc) {
    res.sendResult(doc, 200, "获取公告成功！");
  } else {
    res.sendResult(doc, 500, "获取公告失败！");
  }
};

//修改公告
module.exports.updateOneBoards = async function (req, res, next) {
  let loginUserid = req.query.loginUserid;

  var doc = await findOneByid(administratorsModel, { userid: loginUserid });
  let needRole = '公告管理员';
  let checkPermission = doc.permission.includes(needRole)
  ||doc.permission.includes('超级管理员')
  if(!checkPermission){
    return  res.sendResult(doc, 403, "你没有权限");
  }
  var data = {
    title: req.body.title,
    content: req.body.content,
  };
  console.log(req.query._id);
  var _id = req.body._id;
  var doc = await updateOne(boardsModel, { _id }, req.body);
  if (doc) {
    res.sendResult(doc, 200, "更新公告成功！");
  } else {
    res.sendResult(doc, 500, "更新公告失败！");
  }
};

module.exports.findBoards = async function (req, res, next) {
  let params = req.body.params;
  let page = req.body.page;
  const total = await count(boardsModel, {
    $or: [{
      title: { $regex: req.body.params.title },
      content: { $regex: req.body.params.content }
    }]
  });
  var docs = await find(boardsModel, {
    $or: [{
      title: { $regex: req.body.params.title },
      content: { $regex: req.body.params.content }
    }]
  }, null, {
    skip: (page.pageNumber - 1) * page.pageSize,
    limit: page.pageSize,
    sort: { [page.sortColumn.column]: page.sortColumn.order },
  });
  if (docs) {
    res.sendResult(docs, 200, total);
  } else {
    res.sendResult(docs, 200, "查询失败");
  }
};
